// pages/forgetPwd/forgetPwd.js
import getCheckCode from "../../api/getCheckCode";
import passwordReset from "../../api/passwordReset";
import Keys from "../../config/keys";
import RunData from "../../config/runData";

Page({
    /**
     * 页面的初始数据
     */
    data: {
        idNum:undefined,
        pwd:undefined,
        checkCode:undefined,
        countTimer:"获取验证码",
        disable:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    idNum(e){
        this.setData({
            idNum: e.detail.value
        });
    },
    pwd(e){
        this.setData({
            pwd: e.detail.value
        });
    },
    checkCode(e){
        this.setData({
            checkCode: e.detail.value
        });
    },
    getCheckCode(){
        if(!this.data.idNum||this.data.idNum.length<15){
            wx.showToast({title: '请输入正确身份证', icon: 'none'});
            return
        }
        wx.showLoading({
            title: '发送中',
        });
        new getCheckCode(this.data.idNum,require("../../entity/businessType").重置密码).http(()=>{
            this.setData({
                disable:true
            });
            require("../../utils/countDownTimer")(60*1000,1000,(count)=>{
                this.setData({
                    countTimer:(count/1000+"s"),
                })
            },()=>{
                this.setData({
                    countTimer:"获取验证码",
                    disable:false
                })
            });
        });
    },
    commit(){
        if(!this.data.idNum||this.data.idNum.length<15){
            wx.showToast({title: '请输入正确身份证', icon: 'none'});
            return;
        }
        if(!this.data.pwd||this.data.pwd.length<6){
            wx.showToast({title: '请输入不少于6位密码', icon: 'none'});
            return;
        }
        if(!this.data.checkCode){
            wx.showToast({title: '请输入验证码', icon: 'none'});
            return;
        }
        wx.showLoading();
        new passwordReset(this.data.idNum,this.data.pwd,this.data.checkCode).http((data)=>{
            wx.removeStorage({
                key: Keys.MyUserInfo,
                success: function(res) {
                    wx.showToast({title: '修改成功，请用新密码登录', icon: 'none'});
                    RunData.MyUserInfo = undefined;
                    wx.switchTab({
                        url: '../mainTab/mine/mine'
                    });
                }
            });
        });
    }
})